const birdDom = document.querySelector(".bird");
const birdStyles = getComputedStyle(birdDom);
const birdWidth = parseFloat(birdStyles.width);
const birdHeight = parseFloat(birdStyles.height);
const birdTop = parseFloat(birdStyles.top);
const birdLeft = parseFloat(birdStyles.left);
const gameDom=document.querySelector('.game');
const gameHeight=gameDom.clientHeight;


class Bird extends Rectangle{
    constructor(){
        super(birdWidth,birdHeight,birdLeft,birdTop,0,0,birdDom);
        this.g=1500;//向下的加速度，单位：像素/秒
        this.maxY=gameHeight-landHeight-this.height;
        this.swingStatus=1;//小鸟翅膀状态
        this.timer=null;//小鸟翅膀扇动的计时器
    }
    startSwing(){
        if(this.timer){return;}//如果已经有计时器了，什么都不做
        this.timer=setInterval(()=>{
            this.swingStatus=(this.swingStatus+1)%3 +1;//状态在 1 2 3之间变换
            //跟下面一样的意思
            // this.swingStatus++;
            // if(this.swingStatus==4){
            //     this.swingStatus=1;
            // }
            this.render();
        },100)
    }

    stopSwing(){
        clearInterval(this.timer);
        this.timer=null
    }

    render(){
        super.render();//重用父类逻辑，并追加功能
        this.dom.className=`bird swing${this.swingStatus}`
    }

    move(duration){
        super.move(duration);
        this.ySpeed+=this.g*duration;
    }
    onMove(){
        if(this.top<0){
            this.top=0;
        }else if(this.top>this.maxY){
            this.top=this.maxY;
        }
    }

    jump(){
        this.ySpeed=-500;
    }
}
// var bird=new Bird();
// setInterval(() => {
//     bird.move(16/1000)
// }, 16);